using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Data.OleDb;

namespace Rally_Dekstop
{
    public partial class CreateTestCase : Form
    {
        #region Private Variables
        private string filename = null;
        private bool dataupdated = false;
        private bool populatedata = false;
        private bool bulkUpdate = false;
        string check;
        private ResourceManager m_ResourceManager = Localizer.getInstance().GetResourceManager();
        System.Windows.Forms.ListViewItem listViewItem1 = null;
        string[] testcaseitem = new string[14];
        string[] testCaseDefaults = null;
        bool editMode = false;
        int editItemIndex = -1;
        #endregion

        #region Constructor & Load

        public CreateTestCase()
        {
            InitializeComponent();
            int i = 0;
            for (i = 0; i < RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Types.Count; i++)
            {
                this.cmb_TestCaseType.Items.Add(RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Types[i]);
            }
            for (i = 0; i < RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Priority.Count; i++)
            {
                this.cmb_TestCasePriority.Items.Add(RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Priority[i]);
            }
            for (i = 0; i < RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Method.Count; i++)
            {
                this.cmb_TestCaseMethods.Items.Add(RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Method[i]);
            }
            for (i = 0; i < RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Risk.Count; i++)
            {
                this.cmb_TestCaseRisks.Items.Add(RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Risk[i]);
            }
            for (i = 0; i < RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Package.Count; i++)
            {
                this.cmb_TestCasePackage.Items.Add(RallyDesk.CURRENTWORKSPACE.WorkSpaceSpecifics.TestCase.Package[i]);
            }


            listView_testcases.Columns.Add("Name", 80);
            listView_testcases.Columns.Add("Description", 80);
            listView_testcases.Columns.Add("Notes", 80);
            listView_testcases.Columns.Add("Objective", 80);
            listView_testcases.Columns.Add("Type", 80);
            listView_testcases.Columns.Add("Method", 80);
            listView_testcases.Columns.Add("Priority", 80);
            listView_testcases.Columns.Add("Risk", 80);
            listView_testcases.Columns.Add("Package", 80);
            listView_testcases.Columns.Add("Pre Conditions", 80);
            listView_testcases.Columns.Add("Post Conditions", 80);
            listView_testcases.Columns.Add("Validation Input", 80);
            listView_testcases.Columns.Add("Validation Expected Result", 80);
            listView_testcases.Columns.Add("Uploaded to Rally", 0);

            if (RallyDesk.CURRENTWORKSPACE.SelectedWorkSpaceType == "UseCase")
            {
                this.workproduct_comboBox.Items.Add("Use Case");
                this.workproduct_comboBox.Items.Add("Feature");
                this.workproduct_comboBox.Items.Add("Supplemental Requirement");
                this.workproduct_comboBox.Items.Add("Story");
                this.workproduct_comboBox.Items.Add("Defect");
            }
            else if (RallyDesk.CURRENTWORKSPACE.SelectedWorkSpaceType == "UserStory")
            {
                this.workproduct_comboBox.Items.Add("User Story");

            }

            this.htmlEditor_Expected.IsEditMode = htmlEditor_Validation.IsEditMode = htmlEditor_Desc.IsEditMode =  true;
            this.MdiParent = Rally_Dekstop.Program.RallyMain;
            
        }

        private void CreateTestCase_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
         }
#endregion
       
        #region Private Functions 
        private void SetTestCaseItemValuesfromForm()
        {
            testcaseitem[0] = this.txt_name.Text;
            ////testcaseitem[1] = this.txt_description.Text;
            //testcaseitem[1] = htmlEditor_Desc.GetHTMLFromRTF();

            if (htmlEditor_Desc.isTextEmpty())
                testcaseitem[1] = string.Empty;
            else
                testcaseitem[1] = htmlEditor_Desc.HTMLText;


            
            testcaseitem[2] = this.txt_notes.Text;
            testcaseitem[3] = this.txt_objective.Text;
            testcaseitem[4] = this.cmb_TestCaseType.Text;
            testcaseitem[5] = this.cmb_TestCaseMethods.Text;
            testcaseitem[6] = this.cmb_TestCasePriority.Text;
            testcaseitem[7] = this.cmb_TestCaseRisks.Text;
            testcaseitem[8] = this.cmb_TestCasePackage.Text;
            testcaseitem[9] = this.txt_preConditions.Text;
            testcaseitem[10] = this.txt_postConditions.Text;
            //testcaseitem[11] = htmlEditor_Validation.GetHTMLFromRTF();
            //testcaseitem[11] = this.txt_validationInput.Text;
            if (htmlEditor_Validation.isTextEmpty())
                testcaseitem[11] = string.Empty;
            else
                testcaseitem[11] = htmlEditor_Validation.HTMLText;
            
           // testcaseitem[12] = htmlEditor_Expected.GetHTMLFromRTF();
            //testcaseitem[12] = this.txt_expectedResult.Text;
            if (htmlEditor_Expected.isTextEmpty())
                testcaseitem[12] = string.Empty;
            else
                testcaseitem[12] = htmlEditor_Expected.HTMLText;
         }
        private void ViewTestCaseItemValuesOnForm()
        {
            this.txt_name.Text = testcaseitem[0];
            //this.txt_description.Text = testcaseitem[1];
            htmlEditor_Desc.HTMLText = testcaseitem[1];
            //htmlEditor_Desc.RichTextRTF = htmlEditor_Desc.GetRTFFromHTML();
            
            this.txt_notes.Text = testcaseitem[2];
            this.txt_objective.Text = testcaseitem[3];
            this.cmb_TestCaseType.Text = testcaseitem[4];
            this.cmb_TestCaseMethods.Text = testcaseitem[5];
            this.cmb_TestCasePriority.Text = testcaseitem[6];
            this.cmb_TestCaseRisks.Text = testcaseitem[7];
            this.cmb_TestCasePackage.Text = testcaseitem[8];
            this.txt_preConditions.Text = testcaseitem[9];
            this.txt_postConditions.Text = testcaseitem[10];

            htmlEditor_Validation.HTMLText = testcaseitem[11];
            //this.htmlEditor_Validation.RichTextRTF = htmlEditor_Validation.GetRTFFromHTML();
            //this.txt_validationInput.Text = testcaseitem[11];
            htmlEditor_Expected.HTMLText = testcaseitem[12];
            //this.htmlEditor_Expected.RichTextRTF = htmlEditor_Expected.GetRTFFromHTML();
            //this.txt_expectedResult.Text = testcaseitem[12];

            htmlEditor_Desc.IsEditMode = true;
            htmlEditor_Expected.IsEditMode = true;
            htmlEditor_Validation.IsEditMode = true;
            
        }
        private void TestCaseItemClear()
        {
            testcaseitem[0] = string.Empty;
            testcaseitem[1] = string.Empty;
            testcaseitem[2] = string.Empty;
            testcaseitem[3] = string.Empty;
            testcaseitem[4] = string.Empty;
            testcaseitem[5] = string.Empty;
            testcaseitem[6] = string.Empty;
            testcaseitem[7] = string.Empty;
            testcaseitem[8] = string.Empty;
            testcaseitem[9] = string.Empty;
            testcaseitem[10] = string.Empty;
            testcaseitem[11] = string.Empty;
            testcaseitem[12] = string.Empty;
            testcaseitem[13] = string.Empty;
            
        }
        private void ClearFormItems()
        {
            this.txt_name.Text = string.Empty;
            //this.txt_description.Text = string.Empty;
            htmlEditor_Desc.HTMLText = string.Empty;
            this.txt_notes.Text = string.Empty;
            this.txt_objective.Text = string.Empty;
            this.cmb_TestCaseType.Text = string.Empty;
            this.cmb_TestCaseMethods.Text = string.Empty;
            this.cmb_TestCasePriority.Text = string.Empty;
            this.cmb_TestCaseRisks.Text = string.Empty;
            this.cmb_TestCasePackage.Text = string.Empty;
            this.txt_preConditions.Text = string.Empty;
            this.txt_postConditions.Text = string.Empty;
            this.htmlEditor_Validation.HTMLText = string.Empty;
            //this.txt_validationInput.Text = string.Empty;
            this.htmlEditor_Expected.HTMLText = string.Empty;
            //this.txt_expectedResult.Text = string.Empty;
        
        }
        private void GetListViewItem(ListViewItem tcitem)
        {
            testcaseitem[0] = tcitem.SubItems[0].Text;
            testcaseitem[1] = tcitem.SubItems[1].Text;
            testcaseitem[2] = tcitem.SubItems[2].Text;
            testcaseitem[3] = tcitem.SubItems[3].Text;
            testcaseitem[4] = tcitem.SubItems[4].Text;
            testcaseitem[5] = tcitem.SubItems[5].Text;
            testcaseitem[6] = tcitem.SubItems[6].Text;
            testcaseitem[7] = tcitem.SubItems[7].Text;
            testcaseitem[8] = tcitem.SubItems[8].Text;
            testcaseitem[9] = tcitem.SubItems[9].Text;
            testcaseitem[10] = tcitem.SubItems[10].Text;
            testcaseitem[11] = tcitem.SubItems[11].Text;
            testcaseitem[12] = tcitem.SubItems[12].Text;
            testcaseitem[13] = tcitem.SubItems[13].Text;
            
        }
        private void SetListViewItem(int tcitemindex)
        {
            ListViewItem tcitem = new ListViewItem();
            tcitem = listView_testcases.Items[tcitemindex];
            
            tcitem.SubItems[0].Text = testcaseitem[0]; 
            tcitem.SubItems[1].Text = testcaseitem[1]; 
            tcitem.SubItems[2].Text = testcaseitem[2]; 
            tcitem.SubItems[3].Text = testcaseitem[3]; 
            tcitem.SubItems[4].Text = testcaseitem[4]; 
            tcitem.SubItems[5].Text = testcaseitem[5]; 
            tcitem.SubItems[6].Text = testcaseitem[6]; 
            tcitem.SubItems[7].Text = testcaseitem[7]; 
            tcitem.SubItems[8].Text = testcaseitem[8]; 
            tcitem.SubItems[9].Text = testcaseitem[9]; 
            tcitem.SubItems[10].Text = testcaseitem[10]; 
            tcitem.SubItems[11].Text = testcaseitem[11]; 
            tcitem.SubItems[12].Text = testcaseitem[12];
            tcitem.SubItems[13].Text = testcaseitem[13]; 
        }
        private void UpdateListAfterBulk()
        {
          
            foreach (ListViewItem tcitem in this.listView_testcases.Items)
            {
                if (tcitem.Checked == true)
                {
                    if(testcaseitem[1]!= string.Empty)
                    tcitem.SubItems[1].Text = testcaseitem[1];
                    if (testcaseitem[2] != string.Empty)
                    tcitem.SubItems[2].Text = testcaseitem[2];
                    if (testcaseitem[3] != string.Empty)
                    tcitem.SubItems[3].Text = testcaseitem[3];
                    if (testcaseitem[4] != string.Empty)
                    tcitem.SubItems[4].Text = testcaseitem[4];
                    if (testcaseitem[5] != string.Empty)
                    tcitem.SubItems[5].Text = testcaseitem[5];

                    if (testcaseitem[6] != string.Empty)
                    tcitem.SubItems[6].Text = testcaseitem[6];
                    if (testcaseitem[7] != string.Empty)
                    tcitem.SubItems[7].Text = testcaseitem[7];
                    if (testcaseitem[8] != string.Empty)
                    tcitem.SubItems[8].Text = testcaseitem[8];
                    if (testcaseitem[9] != string.Empty)
                    tcitem.SubItems[9].Text = testcaseitem[9];
                    if (testcaseitem[10] != string.Empty)
                    tcitem.SubItems[10].Text = testcaseitem[10];
                    if (testcaseitem[11] != string.Empty)
                    tcitem.SubItems[11].Text = testcaseitem[11];
                    if (testcaseitem[12] != string.Empty)
                    tcitem.SubItems[12].Text = testcaseitem[12];
                }
            }
        }
        private void EditTestCase()
        {
            if (!bulkUpdate)
            {
                this.editMode = true;
                this.groupBox2.Enabled = false;
                this.btn_add.Text = "Update";
                this.btn_update.Enabled = true;
                this.groupBox_options.Enabled = false;
                this.chk_useDefaults.Enabled = false;

                if (this.listView_testcases.SelectedItems.Count > 0)
                {

                    foreach (ListViewItem tcitem in this.listView_testcases.SelectedItems)
                    {

                        this.editItemIndex = tcitem.Index;
                        this.GetListViewItem(tcitem);
                        this.ViewTestCaseItemValuesOnForm();
                        break;

                    }

                }
            }
            else 
            {
                this.editMode = true;
                this.groupBox2.Enabled = false;
                this.btn_add.Text = "Update";
                this.btn_update.Enabled = true;
                this.groupBox_options.Enabled = false;
                this.chk_useDefaults.Enabled = false;
                this.txt_name.Enabled = false;
                this.cmb_TestCaseMethods.DropDownStyle = ComboBoxStyle.DropDown;
                this.cmb_TestCasePackage.DropDownStyle = ComboBoxStyle.DropDown;
                this.cmb_TestCasePriority.DropDownStyle = ComboBoxStyle.DropDown;
                this.cmb_TestCaseRisks.DropDownStyle = ComboBoxStyle.DropDown;
                this.cmb_TestCaseType.DropDownStyle = ComboBoxStyle.DropDown;

                
                
                
                ClearFormItems();
            }

        }
        private void EditingDone()
        {
            if (!bulkUpdate)
            {
                this.editMode = false;
                this.groupBox2.Enabled = true;
                this.btn_add.Text = "Add";
                this.btn_update.Enabled = false;
                this.groupBox_options.Enabled = true;
                this.btn_setDefaults.Enabled = true;
                this.chk_useDefaults.Enabled = true;
            }
            else 
            {
                this.editMode = false;
                this.groupBox2.Enabled = true;
                this.btn_add.Text = "Add";
                this.btn_update.Enabled = false;
                this.groupBox_options.Enabled = true;
                this.btn_setDefaults.Enabled = true;
                this.chk_useDefaults.Enabled = true;
                this.txt_name.Enabled = true;
                this.cmb_TestCaseMethods.DropDownStyle = ComboBoxStyle.DropDownList;
                this.cmb_TestCasePackage.DropDownStyle = ComboBoxStyle.DropDownList;
                this.cmb_TestCasePriority.DropDownStyle = ComboBoxStyle.DropDownList;
                this.cmb_TestCaseRisks.DropDownStyle = ComboBoxStyle.DropDownList;
                this.cmb_TestCaseType.DropDownStyle = ComboBoxStyle.DropDownList;
                
                

                bulkUpdate = false;
            }



            if (chk_useDefaults.Checked == true)
            {
                this.ClearFormItems();
                this.SetDefaluts();
            }
            else
            {
                this.ClearFormItems();
            }
            
        }
        private void SetDefaluts()
        {
            if (testCaseDefaults != null)
            {
                    if(testCaseDefaults[0] !=string.Empty)
                    this.txt_name.Text = testCaseDefaults[0];

                    if (testCaseDefaults[1] != string.Empty)
                    htmlEditor_Desc.HTMLText = testCaseDefaults[1];

                    if (testCaseDefaults[2] != string.Empty)
                    this.txt_notes.Text = testCaseDefaults[2];

                    if (testCaseDefaults[3] != string.Empty)
                    this.txt_objective.Text = testCaseDefaults[3];

                    if (testCaseDefaults[4] != string.Empty)
                    this.cmb_TestCaseType.Text = testCaseDefaults[4];

                    if (testCaseDefaults[5] != string.Empty)
                    this.cmb_TestCaseMethods.Text = testCaseDefaults[5];

                    if (testCaseDefaults[6] != string.Empty)
                    this.cmb_TestCasePriority.Text = testCaseDefaults[6];

                    if (testCaseDefaults[7] != string.Empty)
                    this.cmb_TestCaseRisks.Text = testCaseDefaults[7];

                    if (testCaseDefaults[8] != string.Empty)
                    this.cmb_TestCasePackage.Text = testCaseDefaults[8];

                    if (testCaseDefaults[9] != string.Empty)
                    this.txt_preConditions.Text = testCaseDefaults[9];
                    if (testCaseDefaults[10] != string.Empty)
                    this.txt_postConditions.Text = testCaseDefaults[10];

                    if (testCaseDefaults[11] != string.Empty)
                    this.htmlEditor_Validation.HTMLText = testCaseDefaults[11];

                    if (testCaseDefaults[12] != string.Empty)
                    this.htmlEditor_Expected.HTMLText = testCaseDefaults[12];
            }

        }
        #endregion

        #region Events
        private void btn_uploadTestCase_Click(object sender, EventArgs e)
        {


            if (this.listView_testcases.CheckedItems.Count < 1)
            {
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/NoRecordsSelected"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // MessageBox.Show("Please select at least one record...", "Rally Desk");
                return;
            }
            if (this.workproduct_comboBox.Text == string.Empty)
            {
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/SelectWorkProduct"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //MessageBox.Show("Please select work product...", "Rally Desk");
                return;
            }
            if (this.SR_ID.Text == string.Empty)
            {
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/ValidWorkProduct"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //MessageBox.Show("Please enter a valid ID...", "Rally Desk");
                return;

            }

            if (MessageBox.Show("Test Cases shall be uploaded to " + RallyDesk.CURRENTWORKSPACE.SelectedProject + " Project. Do you want to continue?", m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            IRallyService service = null;
            if (RallyDesk.CURRENTWORKSPACE.SelectedWorkSpaceType == "UseCase")
                service = new RallyAPIUseCase(RallyDesk.url);
            else
                service = new RallyAPIUserStory(RallyDesk.url);

            //get the workproduct ref
            string typefinal;
            string type = this.workproduct_comboBox.Text.ToLower();

            if (this.workproduct_comboBox.Text == "User Story")
            {
                typefinal = "hierarchicalrequirement";

            }
            else
            {

                string type2 = type.Trim();
                typefinal = type2.Replace(" ", "");

            }

            Program.RallyMain.toolStripStatusLabel.Text = "Verifying Work Product...";
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            string workproductid = service.GetReference(typefinal, this.SR_ID.Text);
            Application.DoEvents();
            Program.RallyMain.toolStripStatusLabel.Text = "Ready";
            this.Cursor = Cursors.Default;
            if (workproductid == "False")
            {
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/CreateTestCase/WrongWorkProduct"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string workproductref = "https://" + RallyDesk.HOSTNAME + ".rallydev.com/slm/webservice/1.05/" + typefinal + "/" + Convert.ToString(workproductid);

            //got workproduct ref

            string result;
            int Recordcount = 0;
            int Count = 0;
            Recordcount = this.listView_testcases.Items.Count;
            Logs Newlog = new Logs();
            ProgressWatch Progress1 = new ProgressWatch("Test Cases Upload in Progress...");

            Newlog.textBox_log.Text = "Test Case Upload Log:" + Environment.NewLine + Environment.NewLine;

            this.Enabled = false;
            Progress1.Show();
            Progress1.PBar.Maximum = 200;// [your known value.]
            Progress1.PBar.Minimum = 0;
            Progress1.PBar.Step = 10;
            Progress1.PBar.PerformStep();
            Progress1.PBar.Step = 200 / Recordcount;
            if (Progress1.WindowState == FormWindowState.Maximized)
                Progress1.WindowState = FormWindowState.Normal;
            Application.DoEvents();
            foreach (ListViewItem tcitem in this.listView_testcases.Items)
            {
                if (Progress1.abortCheck != true)
                {
                    if (tcitem.Checked == true)
                    {
                        Count++;
                        // tc.Description = Utilities.ParseHTMLTextToRallyFormat(htmlEditorDesc.GetHTMLFromRTF());
                        TestCase tc = new TestCase();
                        tc.Name = tcitem.SubItems[0].Text;
                        tc.Description = Utilities.ParseHTMLTextToRallyFormat(tcitem.SubItems[1].Text);

                        tc.Notes = tcitem.SubItems[2].Text;
                        tc.Objective = tcitem.SubItems[3].Text;
                        tc.Type = tcitem.SubItems[4].Text;
                        tc.Method = tcitem.SubItems[5].Text;
                        tc.Priority = tcitem.SubItems[6].Text;
                        tc.Risk = tcitem.SubItems[7].Text;
                        tc.Package = tcitem.SubItems[8].Text;
                        tc.PreConditions = tcitem.SubItems[9].Text;
                        tc.PostConditions = tcitem.SubItems[10].Text;

                        tc.ValidationInput = Utilities.ParseHTMLTextToRallyFormat(tcitem.SubItems[11].Text);
                        tc.ValidationExpectedResult = Utilities.ParseHTMLTextToRallyFormat(tcitem.SubItems[12].Text);
                        if (tcitem.SubItems[13].Text.Equals(string.Empty))
                        {
                            if (tc.Name != string.Empty)
                            {
                                result = service.CreateTestCase(Convert.ToString(RallyDesk.CURRENTWORKSPACE.SelectedProjectRef), workproductref, RallyDesk.Username, tc);
                                if (result == string.Empty)
                                {

                                    Newlog.textBox_log.Text += "Test Case: " + tc.Name + " : ADDED" + Environment.NewLine;
                                    tcitem.SubItems[13].Text = "True";
                                }
                                else
                                {
                                    Newlog.textBox_log.Text += "Invalid Data for " + tc.Name + " :" + result + Environment.NewLine;
                                }


                            }
                            else
                            {
                                Newlog.textBox_log.Text += "Invalid Data for " + tc.Name + Environment.NewLine;
                            }
                        }
                        else
                            Newlog.textBox_log.Text += tc.Name + " has alreay been uploaded once for this template." + Environment.NewLine;

                        Progress1.PBar.PerformStep();

                    }
                    Application.DoEvents();
                }
                else
                    break;

            }

            Newlog.textBox_log.Text += Environment.NewLine;
            Newlog.textBox_log.Text += Convert.ToString(Count) + " Records Processed." + Environment.NewLine;
            Newlog.textBox_log.Text += "Upload Complete." + Environment.NewLine;


            Progress1.Close();
            this.Enabled = true;
            TestCaseItemClear();
            Newlog.ShowDialog();

        }

        private void btn_add_Click(object sender, EventArgs e)
        {

            check = this.txt_name.Text;
            check = check.Trim();
            if (check.Length > 0 || bulkUpdate == true)
            {
                if (btn_add.Text.Equals("Add"))
                {
                    dataupdated = true;
                    this.SetTestCaseItemValuesfromForm();
                    listViewItem1 = new System.Windows.Forms.ListViewItem(testcaseitem, -1);
                    this.listView_testcases.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
                    listViewItem1});
                    this.RecNum_label.Text = Convert.ToString(this.listView_testcases.Items.Count);
                    this.TestCaseItemClear();

                    if (chk_useDefaults.Checked == true)
                    {
                        this.ClearFormItems();
                        this.SetDefaluts();
                    }
                    else
                    {
                        this.ClearFormItems();
                    }


                }
                else
                {
                    dataupdated = true;
                    this.SetTestCaseItemValuesfromForm();
                    if (!bulkUpdate)
                        this.SetListViewItem(editItemIndex);
                    else
                        this.UpdateListAfterBulk();


                    this.EditingDone();
                    this.TestCaseItemClear();

                }

            }
            else
            {
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/CreateTestCase/NameCannotbeEmpty"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


        }

        private void btn_bulk_Click(object sender, EventArgs e)
        {
            if (this.listView_testcases.CheckedItems.Count > 0)
            {
                bulkUpdate = true;
                EditTestCase();
            }
            else
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/CreateTestCase/SelectAtLeastOneItem"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Information);

        }



        private void listView_testcases_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.EditTestCase();
        }

        private void listView_testcases_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if(this.listView_testcases.Items.Count>0)
                this.EditTestCase();
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
                if (editMode)
                    EditingDone();
                TestCaseItemClear();
            
        }

        private void btn_setDefaults_Click(object sender, EventArgs e)
        {
            if (testCaseDefaults == null)
                testCaseDefaults = new string[13];
            
            testCaseDefaults[0] = this.txt_name.Text;
            testCaseDefaults[1] = htmlEditor_Desc.HTMLText;
            testCaseDefaults[2] = this.txt_notes.Text;
            testCaseDefaults[3] = this.txt_objective.Text;
            testCaseDefaults[4] = this.cmb_TestCaseType.Text;
            testCaseDefaults[5] = this.cmb_TestCaseMethods.Text;
            testCaseDefaults[6] = this.cmb_TestCasePriority.Text;
            testCaseDefaults[7] = this.cmb_TestCaseRisks.Text;
            testCaseDefaults[8] = this.cmb_TestCasePackage.Text;
            testCaseDefaults[9] = this.txt_preConditions.Text;
            testCaseDefaults[10] = this.txt_postConditions.Text;
            testCaseDefaults[11] = this.htmlEditor_Validation.HTMLText;
            //testCaseDefaults[11] = this.txt_validationInput.Text;
            
            //testCaseDefaults[12] = this.txt_expectedResult.Text;
            testCaseDefaults[12] = this.htmlEditor_Expected.HTMLText;

            MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/CreateTestCase/DefaultsSaved"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void chk_useDefaults_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_useDefaults.Checked == true)
            {
                this.SetDefaluts();
            
            }
            else
            {
                this.ClearFormItems();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if(this.listView_testcases.CheckedItems.Count>0)    
            {
                if (this.listView_testcases.CheckedItems.Count == this.listView_testcases.Items.Count)
                {                   

                    if (MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/DeleteAllRecordsWarning"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        return;
                
                }
                foreach (ListViewItem tcitem in this.listView_testcases.Items)
                {
                    if (tcitem.Checked == true)
                    {
                        tcitem.Remove();
                    }
                    
                }
                this.dataupdated = true;
                this.RecNum_label.Text = Convert.ToString(this.listView_testcases.Items.Count);
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView_testcases.Items.Count > 0)
                {
                    Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Saving...";
                    string msConn;
                    if (filename == null)
                    {
                        SaveFileDialog op1 = new SaveFileDialog();
                        op1.DefaultExt = ".xls";
                        op1.Filter = "Excel(*.xls)|*.xls";
                        op1.ShowDialog();
                        filename = op1.FileName;
                        if (filename == string.Empty)
                            filename = null;
                            op1.Dispose();
                    }
                    if (filename != null)
                    {

                        if (System.IO.File.Exists(filename))
                        {
                            System.IO.File.Delete(filename);
                        }

                        msConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=\"Excel 8.0;HDR=YES\"";

                        //DataViewer NewData = new DataViewer();
                        OleDbConnection conn = new OleDbConnection();
                        conn.ConnectionString = msConn;
                        conn.Open();


                        System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("CREATE TABLE test " + "([Name] TEXT(255),[Description] LONGTEXT,[Notes] LONGTEXT,[Objective] LONGTEXT, [Type] TEXT(100),[Methods] TEXT(100),[Priority] TEXT(100),[Risk] TEXT(100),[Package] TEXT(100),[PreConditions] LONGTEXT,[PostConditions] LONGTEXT,[ValidationInput] LONGTEXT,[ExpectedResult] LONGTEXT,[UploadFlag] TEXT(100))", conn);
                        cmd.ExecuteNonQuery();
                        foreach (ListViewItem tcitem in this.listView_testcases.Items)
                        {
                            StringBuilder cmdText = new StringBuilder();
                            cmdText.Append("INSERT INTO test VALUES ('");
                            cmdText.Append(tcitem.SubItems[0].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[1].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[2].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[3].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[4].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[5].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[6].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[7].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[8].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[9].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[10].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[11].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[12].Text.Replace("'", "''"));
                            cmdText.Append("','");
                            cmdText.Append(tcitem.SubItems[13].Text.Replace("'", "''"));
                            cmdText.Append("')");
                           // cmd.CommandText = ("INSERT INTO test VALUES ('" + tcitem.SubItems[0].Text.Replace("'", "''") + "','" + tcitem.SubItems[1].Text.Replace("'", "''") + "','" + tcitem.SubItems[2].Text.Replace("'", "''") + "','" + tcitem.SubItems[3].Text.Replace("'", "''") + "','" + tcitem.SubItems[4].Text.Replace("'", "''") + "','" + tcitem.SubItems[5].Text.Replace("'", "''") + "','" + tcitem.SubItems[6].Text.Replace("'", "''") + "','" + tcitem.SubItems[7].Text.Replace("'", "''") + "','" + tcitem.SubItems[8].Text.Replace("'", "''") + "','" + tcitem.SubItems[9].Text.Replace("'", "''") + "','" + tcitem.SubItems[10].Text.Replace("'", "''") + "','" + tcitem.SubItems[11].Text.Replace("'", "''") + "','" + tcitem.SubItems[12].Text.Replace("'", "''") + "','" + tcitem.SubItems[13].Text.Replace("'", "''") + "')");
                            cmd.CommandText = cmdText.ToString();
                            cmd.ExecuteNonQuery();

                        }

                        dataupdated = false;
                        this.Text = "Create Test Cases-[" + filename + "]";
                        Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Saved.";
                        //  MessageBox.Show("Changes Saved.", "Rally Desk");
                        conn.Close();
                        msConn = null;
                    }
                }

            }
                catch (System.IndexOutOfRangeException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Data.OleDb.OleDbException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            catch(Exception)
            {
                throw;
            }
           
            
            finally
            {
               Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Ready";
            }
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                DialogResult res = new DialogResult();


                if (listView_testcases.Items.Count > 0)
                {
                    if (this.dataupdated)
                    {

                        res = MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/UnsavedTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                        if (res == DialogResult.Yes)
                            btn_save.PerformClick();
                        else if (res == DialogResult.Cancel)
                            return;
                    }
                }
                string msConn;
                listView_testcases.Items.Clear();
                this.RecNum_label.Text = Convert.ToString(listView_testcases.Items.Count);
                this.Text = "Create Test Cases";
                filename = null;


                OpenFileDialog op1 = new OpenFileDialog();
                op1.DefaultExt = ".xls";
                op1.Filter = "Excel(*.xls)|*.xls";

                //toolStripStatusLabel.Text = "Uploading test results";

                if (op1.ShowDialog() == DialogResult.OK)
                {
                    filename = op1.FileName;
                    op1.Dispose();
                    msConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=\"Excel 8.0;HDR=YES\"";


                    OleDbConnection conn = new OleDbConnection();
                    conn.ConnectionString = msConn;
                    conn.Open();

                    System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("Select * From [test$]", conn);
                    OleDbDataReader rdr = cmd.ExecuteReader();
                    System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem();

                    while (rdr.Read())
                    {

                        if (rdr.GetValue(0) != System.DBNull.Value)
                            testcaseitem[0] = (rdr.GetString(0));
                        else
                            testcaseitem[0] = "";

                        if (rdr.GetValue(1) != System.DBNull.Value)
                            testcaseitem[1] = (rdr.GetString(1));
                        else
                            testcaseitem[1] = "";


                        if (rdr.GetValue(2) != System.DBNull.Value)
                            testcaseitem[2] = (rdr.GetString(2));
                        else
                            testcaseitem[2] = "";

                        if (rdr.GetValue(3) != System.DBNull.Value)
                            testcaseitem[3] = (rdr.GetString(3));
                        else
                            testcaseitem[3] = "";

                        if (rdr.GetValue(4) != System.DBNull.Value)
                            testcaseitem[4] = (rdr.GetString(4));
                        else
                            testcaseitem[4] = "";
                        if (rdr.GetValue(5) != System.DBNull.Value)
                            testcaseitem[5] = (rdr.GetString(5));
                        else
                            testcaseitem[5] = "";

                        if (rdr.GetValue(6) != System.DBNull.Value)
                            testcaseitem[6] = (rdr.GetString(6));
                        else
                            testcaseitem[6] = "";

                        if (rdr.GetValue(7) != System.DBNull.Value)
                            testcaseitem[7] = (rdr.GetString(7));
                        else
                            testcaseitem[7] = "";

                        if (rdr.GetValue(8) != System.DBNull.Value)
                            testcaseitem[8] = (rdr.GetString(8));
                        else
                            testcaseitem[8] = "";
                        if (rdr.GetValue(9) != System.DBNull.Value)
                            testcaseitem[9] = (rdr.GetString(9));
                        else
                            testcaseitem[9] = "";
                        if (rdr.GetValue(10) != System.DBNull.Value)
                            testcaseitem[10] = (rdr.GetString(10));
                        else
                            testcaseitem[10] = "";

                        if (rdr.GetValue(11) != System.DBNull.Value)
                            testcaseitem[11] = (rdr.GetString(11));
                        else
                            testcaseitem[11] = "";
                        if (rdr.GetValue(12) != System.DBNull.Value)
                            testcaseitem[12] = (rdr.GetString(12));
                        else
                            testcaseitem[12] = "";
                        if (rdr.GetValue(13) != System.DBNull.Value)
                            testcaseitem[13] = (rdr.GetString(13));
                        else
                            testcaseitem[13] = "";


                        listViewItem1 = new System.Windows.Forms.ListViewItem(testcaseitem, -1);
                        this.listView_testcases.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1});



                    }
                    rdr.Close();
                    conn.Close();
                    msConn = null;
                    TestCaseItemClear();

                    this.RecNum_label.Text = Convert.ToString(this.listView_testcases.Items.Count);
                    this.Text = "Create Test Cases-(" + filename + ")";
                    



                }
            }
            catch (System.IndexOutOfRangeException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Data.OleDb.OleDbException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            catch(Exception)
            {
                throw;
            }
           
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void SelectAll_checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.listView_testcases.Items.Count > 0 && SelectAll_checkBox.CheckState == CheckState.Checked)
            {
                foreach (ListViewItem tcitem in this.listView_testcases.Items)
                    tcitem.Checked = true;
                SelectAll_checkBox.Enabled = false;

            }
            else
            {
                SelectAll_checkBox.Enabled = true; ;

            }
        }

        private void deselect_checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.listView_testcases.Items.Count > 0 && deselect_checkBox.CheckState == CheckState.Checked)
            {
                foreach (ListViewItem tcitem in this.listView_testcases.Items)
                    tcitem.Checked = false;
                deselect_checkBox.Enabled = false;

            }
        }

        private void listView_testcases_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.listView_testcases.Items.Count > 0)
            {
                if (this.listView_testcases.CheckedItems.Count < this.listView_testcases.Items.Count)
                {
                    SelectAll_checkBox.CheckState = CheckState.Unchecked;
                    SelectAll_checkBox.Enabled = true;
                }
                else
                    SelectAll_checkBox.Enabled = false;

            }

        }

        private void listView_testcases_ItemChecked(object sender, ItemCheckedEventArgs e) 
        {
            if (this.listView_testcases.Items.Count > 0 && populatedata == true)
            {
                if (this.listView_testcases.CheckedItems.Count < this.listView_testcases.Items.Count)
                {
                    SelectAll_checkBox.CheckState = CheckState.Unchecked;
                    SelectAll_checkBox.Enabled = true;
                }
                else
                    SelectAll_checkBox.Enabled = false;




                if (this.listView_testcases.CheckedItems.Count > 0)
                {
                    deselect_checkBox.CheckState = CheckState.Unchecked;
                    deselect_checkBox.Enabled = true;
                }
                else
                    deselect_checkBox.Enabled = false;



            }
        }

        private void listView_testcases_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView_testcases.Items.Count > 0)
            {
                if (this.listView_testcases.CheckedItems.Count < this.listView_testcases.Items.Count)
                {
                    SelectAll_checkBox.CheckState = CheckState.Unchecked;
                    SelectAll_checkBox.Enabled = true;
                }
                else
                    SelectAll_checkBox.Enabled = false;

            }
        }

        private void CreateTestCase_Shown(object sender, EventArgs e)
        {
            populatedata = true;
        }

        private void CreateTestCase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (dataupdated)
            {
                DialogResult res = new DialogResult();
                res = MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/SaveChanges"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (res == DialogResult.Yes)
                {
                    btn_save.PerformClick();
                }
                else if (res == DialogResult.Cancel)
                    e.Cancel = true;

            }
        }

        private void btn_closeTemplate_Click(object sender, EventArgs e)
        {
            if (dataupdated)
            {
                DialogResult res = new DialogResult();
                res = MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/SaveChanges"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (res == DialogResult.Yes)
                {
                    btn_save.PerformClick();
                }
                if (res == DialogResult.Cancel)
                    return;

            }
            listView_testcases.Items.Clear();
            this.RecNum_label.Text = Convert.ToString( listView_testcases.Items.Count);
            this.Text = "Create Test Cases";
            filename = null;

        }

        private void groupBox_options_Enter(object sender, EventArgs e)
        {

        }

        private void txt_name_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_description_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_SaveAS_Click(object sender, EventArgs e)
        {

            try
            {
                if (this.listView_testcases.Items.Count > 0)
                {
                    Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Saving...";
                    string msConn;
                    SaveFileDialog op1 = new SaveFileDialog();
                    op1.DefaultExt = ".xls";
                    op1.Filter = "Excel(*.xls)|*.xls";
                    if (op1.ShowDialog() == DialogResult.OK)
                    {
                        filename = op1.FileName;
                        if (filename == string.Empty)
                            filename = null;
                        op1.Dispose();

                        if (filename != null)
                        {

                            if (System.IO.File.Exists(filename))
                            {
                                System.IO.File.Delete(filename);
                            }

                            msConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=\"Excel 8.0;HDR=YES\"";

                            //DataViewer NewData = new DataViewer();
                            OleDbConnection conn = new OleDbConnection();
                            conn.ConnectionString = msConn;
                            conn.Open();


                            //System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("CREATE TABLE test " + "([Name] TEXT(200),[Description] TEXT(200),[Notes] TEXT(200),[Objective] TEXT(200), [Type] TEXT(100),[Methods] TEXT(100),[Priority] TEXT(100),[Risk] TEXT(100),[Package] TEXT(100),[PreConditions] TEXT(200),[PostConditions] TEXT(100),[ValidationInput] TEXT(100),[ExpectedResult] TEXT(100),[UploadFlag] TEXT(100))", conn);
                            System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("CREATE TABLE test " + "([Name] TEXT(255),[Description] LONGTEXT,[Notes] LONGTEXT,[Objective] LONGTEXT, [Type] TEXT(100),[Methods] TEXT(100),[Priority] TEXT(100),[Risk] TEXT(100),[Package] TEXT(100),[PreConditions] LONGTEXT,[PostConditions] LONGTEXT,[ValidationInput] LONGTEXT,[ExpectedResult] LONGTEXT,[UploadFlag] TEXT(100))", conn);
                            cmd.ExecuteNonQuery();
                            foreach (ListViewItem tcitem in this.listView_testcases.Items)
                            {
                                StringBuilder cmdText = new StringBuilder();
                                cmdText.Append("INSERT INTO test VALUES ('");
                                cmdText.Append(tcitem.SubItems[0].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[1].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[2].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[3].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[4].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[5].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[6].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[7].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[8].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[9].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[10].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[11].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[12].Text.Replace("'", "''"));
                                cmdText.Append("','");
                                cmdText.Append(tcitem.SubItems[13].Text.Replace("'", "''"));
                                cmdText.Append("')");

                                //cmd.CommandText = ("INSERT INTO test VALUES ('" + tcitem.SubItems[0].Text.Replace("'", "''") + "','" + tcitem.SubItems[1].Text.Replace("'", "''") + "','" + tcitem.SubItems[2].Text.Replace("'", "''") + "','" + tcitem.SubItems[3].Text.Replace("'", "''") + "','" + tcitem.SubItems[4].Text.Replace("'", "''") + "','" + tcitem.SubItems[5].Text.Replace("'", "''") + "','" + tcitem.SubItems[6].Text.Replace("'", "''") + "','" + tcitem.SubItems[7].Text.Replace("'", "''") + "','" + tcitem.SubItems[8].Text.Replace("'", "''") + "','" + tcitem.SubItems[9].Text.Replace("'", "''") + "','" + tcitem.SubItems[10].Text.Replace("'", "''") + "','" + tcitem.SubItems[11].Text.Replace("'", "''") + "','" + tcitem.SubItems[12].Text.Replace("'", "''") + "','" + tcitem.SubItems[13].Text.Replace("'", "''") + "')");
                                cmd.CommandText = cmdText.ToString();
                                cmd.ExecuteNonQuery();

                            }

                            dataupdated = false;
                            this.Text = "Create Test Cases-[" + filename + "]";
                            Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Saved.";
                            //  MessageBox.Show("Changes Saved.", "Rally Desk");
                            conn.Close();
                            msConn = null;
                        }
                    }
                } 
            }
            catch (System.IndexOutOfRangeException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Data.OleDb.OleDbException )
            {
                
                MessageBox.Show(m_ResourceManager.GetString("memo://rallydesk/InvalidTemplate"), m_ResourceManager.GetString("memo://rallydesk/RallyDeskCaption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            catch(Exception)
            {
                throw;
            }
           
            
            finally
            {
                Rally_Dekstop.Program.RallyMain.toolStripStatusLabel.Text = "Ready";
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
#endregion
    
    }
}